﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Pcb.DBUtility;
using Pcb.Common;
using System.Data.SqlClient;

namespace Pcb.DAL
{
    /// <summary>
    /// 系统参数
    /// </summary>
    public partial class OptionDAL
    {

        /// <summary>        
        /// 获得查询分页数据        
        /// </summary>
        /// <param name="pageSize">每页显示多少</param>
        /// <param name="pageIndex">显示第几页</param>
        /// <param name="strWhere">查询条件,如：name='vincent'</param>
        /// <param name="filedOrder">排序字段，多个则用“,”隔开</param>
        /// <param name="recordCount">总记录数</param>
        /// <returns></returns>
        public DataSet GetList(int pageSize, int pageIndex, string strWhere, string filedOrder, out int recordCount)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * FROM t_option");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            recordCount = Convert.ToInt32(DbHelperSQL.GetSingle(PagingHelper.CreateCountingSql(strSql.ToString())));
            return DbHelperSQL.Query(PagingHelper.CreatePagingSql(recordCount, pageSize, pageIndex, strSql.ToString(), filedOrder));
        }

        /// <summary>
        /// 修改一列数据
        /// </summary>
        /// <param name="id">自增ID</param>
        /// <param name="strValue">如: keyValue='0.9' </param>
        public void UpdateField(int id, string strValue)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update t_option set " + strValue);
            strSql.Append(" where id=" + id);
            DbHelperSQL.ExecuteSql(strSql.ToString());
        }

        /// <summary>
        /// 修改一列数据
        /// </summary>
        /// <param name="strWhere">如：keyType='板子厚度' </param>
        /// <param name="strValue">如: keyValue='0.9' </param>
        /// <returns></returns>
        public bool UpdateField(string strWhere, string strValue)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update t_option set " + strValue);
            strSql.Append(" where " + strWhere + " ");
            int rowsAffected = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rowsAffected > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 根据类名及键值名获取值
        /// </summary>
        /// <param name="typeName">类别名称</param>
        /// <param name="keyName">键值名称</param>
        /// <returns></returns>
        public float GetValue(string typeName, string keyName)
        {
            float keyValue = 0;
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 1 ID,TypeName,KeyName,KeyValue from t_option ");
            strSql.Append(" where IsDelete = 0 and TypeName = '" + typeName + "' and KeyName = '" + keyName + "' ");
                      
            DataSet ds = DbHelperSQL.Query(strSql.ToString());
            if (ds.Tables[0].Rows.Count > 0)
            {
                keyValue = float.Parse(ds.Tables[0].Rows[0]["KeyValue"].ToString());
            }

            return keyValue;
        }

    }
}
